package fr.km.client.view.impl;

import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

import fr.km.client.presenter.api.IArticleEditorPresenter;
import fr.km.client.view.api.IArticleEditorView;

public class ArticleEditorView extends Composite implements IArticleEditorView/*, HasSaveHandlers, HasCloseHandlers<ArticleEditorView>, ClickHandler*/{
	private IArticleEditorPresenter presenter;
	
	private Button saveButton;
    private Button cancelButton;
    private TextBox title;
    private TextBox tags;
    private TextArea knowledge;
    
    private DialogBox popup;
    
	public ArticleEditorView() {
		
		// Create a table to layout the form options
	    FlexTable layout = new FlexTable();
	    layout.setCellSpacing(6);

	    title = new TextBox();
	    title.setSize("30em", "25px");
	    tags = new TextBox();
	    tags.setSize("30em", "25px");
	    knowledge = new TextArea();
	    knowledge.setSize("30em", "10em");
	    saveButton = new Button("Save");
	    cancelButton = new Button("Cancel");
	    
	    layout.setHTML(0, 0, "Title:");
	    layout.setWidget(0, 1, title);
	    layout.setHTML(1, 0, "Tags:");
	    layout.setWidget(1, 1, tags);
	    layout.setHTML(2, 0, "Knowledge");
	    layout.setWidget(2, 1, knowledge);
	    layout.setWidget(3, 0, saveButton);
	    layout.setWidget(3, 1, cancelButton);

	    
//	    saveButton.addClickHandler(this);
//	    cancelButton.addClickHandler(this);
	    
//	    displayTitle(article.getTitle());
//	    displayTags(article.getTags());
//	    displayKnowledge(article.getContent());
	    
	    
		initWidget(layout);
		
	}

//	@Override
//	public HandlerRegistration addSaveHandler(SaveHandler handler) {
//		return addHandler(handler, SaveEvent.getType());
//	}
//
//	@Override
//	public HandlerRegistration addCloseHandler(
//			CloseHandler<ArticleEditorView> handler) {
//		return addHandler(handler, CloseEvent.getType());
//	}
//
//	@Override
//	public void onClick(ClickEvent event) {
//		Button source = (Button) event.getSource();
//		if(source == saveButton) {
//			presenter.save();
//		} else {
//			presenter.cancel();
//		}
//	}
	
	public String getTitle() {
		return title.getText();
	}
	
	public ArrayList<String> getTags() {
		String text = tags.getText();
		String[] split = text.split(",");
		ArrayList<String> tagList = new ArrayList<String>();
		for(String tag : split) {
			String trim = tag.trim();
			if(!trim.equals("")) {
				tagList.add(trim);
			}
		}
		
		return tagList;
	}
	
	public String getKnowledge() {
		return knowledge.getText();
	}

	@Override
	public void displayKnowledge(String knowledge) {
		this.knowledge.setText(knowledge);		
	}

	@Override
	public void displayTags(ArrayList<String> tags) {
		Iterator<String> iterator = tags.iterator();
		String tagString = new String();
	    while(iterator.hasNext()) {
	    	tagString += iterator.next();
	    	if(iterator.hasNext()) {
	    		tagString += ", ";
	    	}
	    }
	    
	    this.tags.setText(tagString);
	}

	@Override
	public void displayTitle(String title) {
		this.title.setText(title);		
	}

	@Override
	public void setPresenter(IArticleEditorPresenter presenter) {
		this.presenter = presenter;		
	}

	@Override
	public void close() {
		popup.hide();
	}

	@Override
	public void display() {
		popup = new DialogBox();
		popup.setText("Edit...");
		popup.setWidget(this);
		popup.setModal(true);
		popup.center();
		popup.show();
	}

	@Override
	public HasClickHandlers getCancelButton() {
		return cancelButton;
	}

	@Override
	public HasClickHandlers getSaveButton() {
		return saveButton;
	}
}
